package com.si.controller.manage;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.si.entity.InfoEnclosure;
import com.si.entity.OperationLog;
import com.si.service.*;
import com.si.util.FileHandleUtil;
import com.si.util.IpAddressUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.si.entity.Info;
import com.si.entity.InfoTypeClassA;
import com.si.util.FileUpload;
import com.si.util.Paging;

/**
 * 信息管理
 * @author Dell
 *
 */
@Controller
@RequestMapping("info/manage")
public class InfoManageController {

	@Autowired
	private InfoService infoService;
	
	@Autowired
	private InfoTypeClassAService infoTypeClassAService;
	@Autowired
	private OperationLogService operationLogService;
	@Autowired
	private InfoEnclosureService infoEnclosureService;
	/**
	 * 
	 * @return
	 */
	@RequestMapping(value="list/{infoTypeClassAId}")
	public String list(
			ModelMap modelMap,
			@PathVariable(value="infoTypeClassAId") Integer infoTypeClassAId,
			@RequestParam(value = "curPage", defaultValue = "0", required = false) Integer curPage,
			@RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(value = "keywords", required = false) String keywords) {
		
		Map<String, Object> parameterMap=new HashMap<String, Object>();
		parameterMap.put("infoTypeClassA.id", infoTypeClassAId);
		if(StringUtils.isNoneBlank(keywords)) {
			parameterMap.put("cnName||enName||rtwName", keywords);
		}
		Map<String, Object> orderMap=new HashMap<String, Object>();
		orderMap.put("createDate", "desc");
		Paging<Info> paging = infoService.createQueryPaging(parameterMap, orderMap, curPage, pageSize);
		modelMap.put("paging", paging);
		//查询一级分类的信息
		InfoTypeClassA infoTypeClassA=(InfoTypeClassA)infoTypeClassAService.getObjectById(infoTypeClassAId);
		modelMap.put("infoTypeClassA", infoTypeClassA);
		//
		modelMap.put("keywords", keywords);
		return "managePage/info/list";
	}
	
	/**
	 * 进入到添加页面
	 * @return
	 */
	@RequestMapping(value="initAdd/{infoTypeClassAId}")
	public String initAdd(ModelMap modelMap,@PathVariable(value="infoTypeClassAId") Integer infoTypeClassAId) {
		InfoTypeClassA infoTypeClassA=(InfoTypeClassA)infoTypeClassAService.getObjectById(infoTypeClassAId);
		modelMap.put("infoTypeClassA", infoTypeClassA);
		return "managePage/info/update";
	}
	
	/**
	 * 进入到修改页面
	 * @return
	 */
	@RequestMapping(value="update/{infoTypeClassAId}/{id}")
	public String update(
			ModelMap modelMap,
			@PathVariable(value="infoTypeClassAId") Integer infoTypeClassAId,
			@PathVariable(value="id") Integer id) {
		Info info=(Info)infoService.getObjectById(id);
		modelMap.put("info", info);
		//查询二级分类的信息
		InfoTypeClassA infoTypeClassA=(InfoTypeClassA)infoTypeClassAService.getObjectById(infoTypeClassAId);
		modelMap.put("infoTypeClassA", infoTypeClassA);
		return "managePage/info/update";
	}
	
	/**
	 * 保存数据
	 * @return
	 */
	@RequestMapping(value="save")
	public String save(HttpServletRequest request,
					   ModelMap modelMap,
					   @RequestParam(value = "id") Integer id,
					   @RequestParam(value = "cnName") String cnName,
					   @RequestParam(value = "cnContent") String cnContent,
					   @RequestParam(value = "infoTypeClassAId") Integer infoTypeClassAId,
					   @RequestParam(value = "fileInput") MultipartFile[] file,
					   @RequestParam(value = "volume", defaultValue = "0" ,required = false) Integer volume) throws IOException {
		Info info=(Info)infoService.getObjectById(id);
		info.setCnName(cnName);
		info.setCnContent(cnContent);
		info.setCreateDate(new Date());
		info.setInfoTypeClassA(new InfoTypeClassA(infoTypeClassAId));
		info.setVolume(volume);
		InfoTypeClassA id1 = (InfoTypeClassA) infoTypeClassAService.getObjectById(infoTypeClassAId);
		infoService.saveOrUpdate(info);
		for (MultipartFile multipartFile : file) {
			InfoEnclosure enclosure = new InfoEnclosure();
			String filename = FileHandleUtil.Upset(multipartFile);
			enclosure.setInfoid(info.getId());
//			String name = multipartFile.getName();
			String originalFilename = multipartFile.getOriginalFilename();
			enclosure.setFilename(originalFilename);
			enclosure.setUrl(FileHandleUtil.upload(multipartFile.getInputStream(), "upload/info_enclosuer/", filename, request));
			infoEnclosureService.save(enclosure);
		}

		OperationLog operationLog = new OperationLog();
		operationLog.setMiaoshu("执行成功！");
		operationLog.setOperationdetails("添加/修改信息管理-"+id1.getName()+"-"+"\""+info.getCnName()+"\"");
		operationLog.setOperationtime(new Date().toString());
		operationLog.setSensitive("信息管理");
		String ip = IpAddressUtil.getIpAddress(request);
		operationLog.setIpadderse(ip);
		String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
		operationLog.setUsername(sysUser);
		operationLogService.save(operationLog);
		return "redirect:/info/manage/list/"+infoTypeClassAId+".html";
	}
	
	/**
	 * 根据id 删除
	 * @param id
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value="delete/{infoTypeClassAId}/{id}")
	public String delete(
			@PathVariable(value="infoTypeClassAId") Integer infoTypeClassAId,
			@PathVariable(value="id") Integer id,
			HttpServletRequest request,	RedirectAttributes redirectAttributes){
		Info info=(Info)infoService.getObjectById(id);
		InfoTypeClassA id1 = (InfoTypeClassA) infoTypeClassAService.getObjectById(infoTypeClassAId);
		OperationLog operationLog = new OperationLog();
		operationLog.setMiaoshu("执行成功");
		operationLog.setOperationdetails("删除信息管理-"+id1.getName()+"-"+"\""+info.getCnName()+"\"");
		operationLog.setOperationtime(new Date().toString());
		operationLog.setSensitive("信息管理");
		String ip = IpAddressUtil.getIpAddress(request);
		operationLog.setIpadderse(ip);
		String sysUser = (String) request.getSession().getAttribute("session_sysUser_name");
		operationLog.setUsername(sysUser);
		operationLogService.save(operationLog);
		infoService.delete(info);
		redirectAttributes.addFlashAttribute("message", "删除成功");
		return "redirect:/info/manage/list/"+infoTypeClassAId+".html";
	}
}
